package com.mu;

import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;

/**
 * @Classname LambdaDemo01
 * @Description TODO
 * @Date 2023/1/8 16:02
 * @Created by JiangMuyun
 */
public class LambdaDemo01 {
    public static void main(String[] args) {
        /*示例1
        //创建新线程普通写法：匿名内部类写法
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("新线程中run方法被执行了");
            }
        });
        thread1.start();
        //创建新线程简化写法：Lambda表达式写法
        Thread thread02 = new Thread(()->{
            System.out.println("新线程中run方法被执行了");
        });
        thread02.start();*/
        /*===================================================*/

        /*示例2*/
        //匿名内部类写法
        /*System.out.println(
                calculateNum(new IntBinaryOperator() {
            @Override
            public int applyAsInt(int left, int right) {
                return left + right;
            }
        }));*/
        //Lambda表达式写法
        /*System.out.println(
                calculateNum((int left, int right) -> {
            return left + right;
        }));*/
        //Lambda表达式快捷键简化
        /*System.out.println(
                calculateNum((left, right) -> left + right));*/
        /*===================================================*/

        /*示例3*/
        //匿名内部类写法
        /*printNum(new IntPredicate() {
            @Override
            public boolean test(int value) {
                return value%2==0;
            }
        });*/
        //Lambda表达式写法
        /*printNum((int value)->{
            return value%2==0;
        });*/
        //Lambda表达式快捷键简化
        //printNum(value -> value%2==0);
        /*===================================================*/

        /*示例4*/
        //匿名内部类写法
        /*System.out.println(typeConver(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.valueOf(s);
            }
        }));*/
        //Lambda表达式写法
        /*System.out.println(
                typeConver(
                        (Function<String, Integer>) s -> {
                    return Integer.valueOf(s);
                })
        );*/
        /*System.out.println(
                typeConver((String s) ->{
                    return s+"666";
                })
        );*/
        //Lambda表达式快捷键简化
       /* System.out.println(typeConver((Function<String, Integer>) s -> Integer.valueOf(s)));*/
        /*===================================================*/

        /*示例5*/
        //匿名内部类写法
        /*foreachArr(new IntConsumer() {
            @Override
            public void accept(int value) {
                System.out.println(value);
            }
        });*/
        //Lambda表达式写法
        /*foreachArr((int value)->{
            System.out.println(value);
        });*/
        //Lambda表达式快捷键简化
        //foreachArr(value -> System.out.println(value));
        /*===================================================*/

        /*示例6*/
        //匿名内部类写法
        //Lambda表达式写法
        //Lambda表达式快捷键简化
        /*===================================================*/

    }

    /*示例2涉及到的方法*/
    public static int calculateNum(IntBinaryOperator operator){
        int a = 10;
        int b = 20;
        return operator.applyAsInt(a, b);
    }
    /*示例3涉及到的方法*/
    public static void printNum(IntPredicate predicate){
        int arr[] = {1,2,3,4,5,6,7,8,9,10};
        for (int i: arr) {
            if (predicate.test(i)){
                System.out.println(i);
            }
        }
    }
    /*示例4涉及到的方法*/
    public static <R> R typeConver(Function<String, R> function){
        String str = "1235";
        R result = function.apply(str);
        return result;
    }
    /*示例5涉及到的方法*/
    public static void foreachArr(IntConsumer consumer){
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        for (int i : arr){
            consumer.accept(i);
        }
    }
}
